package com.jiwei.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jiwei.system.dao.MemberLoginMapper;
import com.jiwei.system.dao.RepairTaskMapper;
import com.jiwei.system.pojo.Log;
import com.jiwei.system.pojo.Member;
import com.jiwei.system.pojo.MemberLogin;
import com.jiwei.system.pojo.Task;
import com.jiwei.system.serviceImpl.RepairServiceImpl;
import com.jiwei.system.utils.CookieUtil;
import com.jiwei.system.utils.JWTUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = {"member", "operator", "admin"},method = RequestMethod.POST)
@RestController
public class RepairController {
    @Resource(name = "RepairService")
    private RepairServiceImpl repairService;

    @Resource
    private RepairTaskMapper repairTaskMapper;

//    @PostMapping(value = "sendPicture")
//    public String acceptPicture(@RequestParam("files") List<MultipartFile> files, HttpServletRequest request){
//        repairService.getPicUrl(files);
//        return "{" +
//                "\"statusCode\":\"" + 200 + '\"' +
//                ", \"message\":\"" + "报修成功" + "\""+
//                '}';
//
//    }


    @PostMapping(value = "sendPicture")
    public String acceptPicture(Task task, HttpServletRequest request){
//        repairService.getPicUrl(task);
        List<MultipartFile> files = task.getFiles();
        repairService.getPicUrl(files);
        System.out.print("task:");
        System.out.println(task);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "报修成功" + "\""+
                '}';

    }


    @PostMapping("getMyTaskList")
    public Map<String,Object> getMyTaskList(HttpServletRequest request){
//        String token = CookieUtil.getCookieValueNamedToken(request);
//        String account = JWTUtil.getClaimNamedAccount(token);
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        List<Task> TaskList = repairService.memberQuerySelfTask(account);
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", TaskList.size());
        res.put("data", TaskList);
        res.put("msg", "");
        return res;
    }

    @PostMapping("getTaskDetail")
    public Map<String,Object> getMyTaskDetail(@RequestParam("id") int id, HttpServletRequest request){
        List<Task> TaskDetail = repairService.getTaskDetailById(id);
//        String log = repairService.getTaskLog(id);
//        System.out.println(log);
        Map<String, Object> res= new HashMap<>();
        res.put("code", 0);
        res.put("count", TaskDetail.size());
        res.put("data", TaskDetail);
//        res.put("log",log);
        res.put("msg", "");
        return res;
    }


    @PostMapping("getAllTaskList")
    public Map<String,Object> getAllTaskList(@RequestParam(value = "page")int page,@RequestParam(value = "limit") int limit, HttpServletRequest request){
//        String token = CookieUtil.getCookieValueNamedToken(request);
//        String account = JWTUtil.getClaimNamedAccount(token);
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        Map<String, Object> res= new HashMap<>();
            List<Task> TaskList = repairService.memberQueryAllTask(page,limit);
            int totalcount = repairTaskMapper.queryAlltaskNum();
            res.put("code", 0);
            res.put("totalcount",totalcount);
            res.put("data", TaskList);
            res.put("msg", "");
            return res;
    }

    @PostMapping("getOfflineTaskList")   //各种状态的线下任务
    public Map<String,Object> getOfflineTaskList(@RequestParam(value = "page")int page,@RequestParam(value = "limit") int limit, @RequestParam(value = "method") String method, @RequestParam(value = "status") String status,HttpServletRequest request){
        Map<String, Object> res= new HashMap<>();
        if(status.equals("所有状态")){
            List<Task> TaskList = repairService.memberQueryAllOfflineTask(page,limit,method);
            int totalcount = repairTaskMapper.queryAllOfflinetaskNum(method);
            res.put("code", 0);
            res.put("totalcount",totalcount);
            res.put("data", TaskList);
            res.put("msg", "");
            return res;
        }else{
            List<Task> TaskList = repairService.memberQueryOfflineTaskByStatus(page,limit,method,status);
            int totalcount = repairTaskMapper.queryOfflineTaskNumByStatus(method,status);
            res.put("code", 0);
            res.put("totalcount",totalcount);
            res.put("data", TaskList);
            res.put("msg", "");
            return res;
        }

    }

    @PostMapping("getOnlineTaskList")  //各种状态的线上任务
    public Map<String,Object> getOnlineTaskList(@RequestParam(value = "page")int page,@RequestParam(value = "limit") int limit, @RequestParam(value = "method") String method, @RequestParam(value = "status") String status, HttpServletRequest request){
        Map<String, Object> res= new HashMap<>();
        if(status.equals("所有状态")){
            List<Task> TaskList = repairService.memberQueryAllOnlineTask(page,limit,method);
            int totalcount = repairTaskMapper.queryAllOnlinetaskNum(method);
            res.put("code", 0);
            res.put("totalcount",totalcount);
            res.put("data", TaskList);
            res.put("msg", "");
            return res;
        }else{
            List<Task> TaskList = repairService.memberQueryOnlineTaskByStatus(page,limit,method,status);
            int totalcount = repairTaskMapper.queryOnlineTaskNumByStatus(method,status);
            res.put("code", 0);
            res.put("totalcount",totalcount);
            res.put("data", TaskList);
            res.put("msg", "");
            return res;
        }

    }

    @PostMapping("releaseOnlineTask")  //发布线上任务
    public String releaseTask(@RequestParam(value = "id")int id, @RequestParam(value = "idList") List<String> idList, @RequestParam(value = "nameList") List<String> nameList, HttpServletRequest request){
        repairService.setParticipant(id,idList,nameList);
        repairService.modifyRepairStatus(id,"处理中");
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "发布成功" + "\""+
                '}';
    }


    @PostMapping(value = {"finishOnlineTask","cancelOnlineTask"})
    public String modifyTaskStatus(HttpServletRequest request,@RequestParam(value = "id") int id){
        String url = request.getServletPath();
        Map<String, String> urlMap = new HashMap<String, String>() {
            {
                put("/admin/finishOnlineTask", "已完成");
                put("/admin/cancelOnlineTask", "取消");
                put("/operator/finishOnlineTask", "已完成");
                put("/operator/cancelOnlineTask", "取消");
            }

        };
        repairService.modifyRepairStatus(id,urlMap.get(url));
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "修改成功" + "\""+
                '}';

    }


    @PostMapping(value = "modifyTaskLog")
    public String modifyTaskLog(HttpServletRequest request,@RequestBody Log taskLog){
        int id = taskLog.getId();
        JSONArray tasklog = taskLog.getTaskLog();
        Map<String,Object> attritube = new HashMap<>();
        attritube.put("id",id);
        attritube.put("tasklog",tasklog);
        repairTaskMapper.modifyTaskLog(attritube);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "修改成功" + "\""+
                '}';

    }





}
